#pragma once

#ifndef EVENTENGINEADAPTER_H
#define EVENTENGINEADAPTER_H


#include "SmartTrade\SmartTradecUserApiStruct.h"
#include "BaseTradeProvider.h"
#include <loki\SmartPtr.h>

namespace SmartTrade{
	template<typename T,typename Event>
	struct EventMonitor{
		union{
			int Addr;
			void (T::*OnEvent)(const Event* pEvent);
		};
		char* EventCode;
	};
	
	struct EventEngineAdapter{
		typedef Loki::SmartPtr<EventMonitor<EventEngineAdapter,SmartTradecTransaction> >      Ptr_TransactionMonitor;
		typedef Loki::SmartPtr<EventMonitor<EventEngineAdapter,SmartTradecArbitTransaction> > Ptr_ArbitTransactionMonitor;
		EventEngineAdapter(Ptr_TradeProviderMap pTradeProvMap){
			
			pTradeProvMap_ = pTradeProvMap;
			pTransMon_			= NULL;
			pArbitTransMon_		= NULL;

			Start();
		}

		void Start(){
			pTransMon_ = new EventMonitor<EventEngineAdapter,SmartTradecTransaction>();
			pTransMon_->OnEvent = &EventEngineAdapter::On_Transaction;
			strcpy(pTransMon_->EventCode , MARKET_DATA_TYPE_QUOTE);

			pArbitTransMon_ = new EventMonitor<EventEngineAdapter,SmartTradecArbitTransaction>();
			pArbitTransMon_->OnEvent = &EventEngineAdapter::On_Transaction;
			strcpy(pArbitTransMon_->EventCode , MARKET_DATA_TYPE_TRADE);
		}

		void RegisterToEventEngine(){
			
			//RegisterEventMonitor(pTransMon_->EventCode,pTransMon_->Addr,this);
			//RegisterEventMonitor(pArbitTransMon_->EventCode,pArbitTransMon_->Addr,this);
		}
		
		void On_Transaction(const SmartTradecTransaction* pTrans){

			for(map<string,Ptr_BaseTradeProvider>::iterator i = pTradeProvMap_->begin();i != pTradeProvMap_->end(); ++i){
				i->second->SendTransaction(pTrans);
			}
		}
		void On_Transaction(const SmartTradecArbitTransaction* pArbitTrans){
			for(map<string,Ptr_BaseTradeProvider>::iterator i = pTradeProvMap_->begin();i != pTradeProvMap_->end(); ++i){
				i->second->SendTransaction(pArbitTrans);
			}
		}
		

	private:
		Ptr_TransactionMonitor			   pTransMon_;
		Ptr_ArbitTransactionMonitor		   pArbitTransMon_;

		Ptr_TradeProviderMap pTradeProvMap_; 
	};
	
	typedef Loki::SmartPtr<EventEngineAdapter> Ptr_EventEngineAdapter; 
}

#endif // EVENTENGINEADAPTER_H